<style lang="less">
@import '../styles/menu.less';
</style>
<template>
    <Menu ref="sideMenu" :active-name="$route.name" :open-names="openNames" :theme="menuTheme" width="auto" z @on-select="changeMenu">
        <treeMenu :list="menuList" :itemTitle="itemTitle" :iconSize="iconSize"></treeMenu>
    </Menu>
</template>
<script>
const treeMenu = {
            props: ['list','itemTitle','iconSize'],
            name:'treeMenu',
            components:{
                treeMenu:treeMenu
            },
            template: `
            <div>
            <template v-for="item in list">
            <MenuItem v-if="item.children.length<1||!item.children " :name="item.name" :key="'menuitem' + item.name">
                <Icon :type="item.icon" :size="iconSize" :key="'menuicon' + item.name"></Icon>
                <span class="layout-text" :key="'title' + item.name">{{ itemTitle(item) }}</span>
            </MenuItem>

            <Submenu v-if="item.children.length > 0" :name="item.name" :key="item.name">
                <template slot="title">
                    <Icon :type="item.icon" :size="iconSize"></Icon>
                    <span class="layout-text">{{ itemTitle(item) }}</span>
                </template>
                    <treeMenu :list="item.children" :itemTitle="itemTitle" :iconSize="iconSize">
                    </treeMenu>
            </Submenu>
            </template>
            </div>`
        };
export default {
    components: {
        treeMenu:treeMenu
    },
    name: 'sidebarMenu',
    props: {
        menuList: Array,
        iconSize: Number,
        menuTheme: {
            type: String,
            default: 'dark'
        },
        openNames: {
            type: Array
        }
    },
    methods: {
        changeMenu(active) {
            this.$emit('on-change', active);
        },
        itemTitle(item) {
            if (typeof item.title === 'object') {
                return this.$t(item.title.i18n);
            } else {
                return item.title;
            }
        }
    },
    updated() {
        this.$nextTick(() => {
            if (this.$refs.sideMenu) {
                this.$refs.sideMenu.updateOpened();
            }
        });
    }

};
</script>